import http from "@/utils/request";
import type { SysUserForm, SysUserInfo, SysUserQuery } from "./types";
import type { AxiosPromise } from 'axios'

/**
 * 登录成功后获取用户信息（昵称、头像、权限集合和角色集合）
 */
export function getUserInfo(): AxiosPromise<SysUserInfo> {
  return http({
    url: "/api/v1/user/me",
    method: "get",
  });
}

/**
 * 获取用户角色列表
 */
export function getCurrRoles() {
  return http({
    url: "/api/v1/role/currUserRoles",
    method: "get",
  });
}
/**
 * 获取用户分页列表
 *
 * @param queryParams
 */
export const getUserPage = (queryParams: SysUserQuery) => {
  return http({
    url: "/api/v1/user/page",
    method: "get",
    params: queryParams,
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

/**
 * 获取用户表单详情
 *
 * @param userId
 */
export const getUserForm = (userId: number)/*: AxiosPromise<SysUserForm>*/ =>{
  return http({
    url: "/api/v1/user/getUserForm",
    method: "get",
    params: {userId: userId},
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

/**
 * 添加用户
 *
 * @param data
 */
export const addUser = (data: any) => {
  return http({
    url: "/api/v1/user/add",
    method: "post",
    data: data,
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

/**
 * 修改用户
 *
 * @param id
 * @param data
 */
export const updateUser = (id: number, data: SysUserForm) => {

  return http({
    url: "/api/v1/user/update",
    method: "put",
    data: data,
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

export const deleteUser = (id: number) => {
  return http({
    url: "/api/v1/user/delete",
    method: "delete",
    params: {uid: id},
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

/**
 * 修改用户密码
 *
 * @param id
 * @param password
 */
export const updateUserPassword = (id: number, password: string) => {
  return http({
    url: "/api/v1/users/" + id + "/password",
    method: "patch",
    params: { password: password },
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

/**
 * 删除用户
 *
 * @param ids
 */
export const deleteUsers = (ids: string[]) => {
  console.log("deleteUsers_ids:",ids);
  return http({
    url: "/api/v1/users/",
    method: "delete",
    data: ids,
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

